#!/bin/sh
#
#   http://code.google.com/media-translate/
#   Copyright (C) 2010  Serge A. Timchenko
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# 


# Translate CGI module
# 'guzei.com' plug-in
#
# version: 1.0 
#
# http://guzei.com/online_radio/listen.php?online_radio_id=5325
# http://guzei.com/online_tv/watch.php?online_tv_id=1781
#

stream_type=''
stream_url=''
server_type=''

if echo "${arg_url}" | grep -q -s '\(www\.\)*guzei\.com/online_radio/listen.php?online_radio_id=[0-9].*$'; then 

  local host_response=`$MSDL -q -o ${TMPFILE} -p http --useragent "${USERAGENT}" "${arg_url}" 2>&1`
  
  if [ -f ${TMPFILE} ]; then
    arg_url=`grep "player(" "$TMPFILE" | sed "s/.*('//;s/','.*//"`;
    local station_title=`grep "<h1" "$TMPFILE" | sed "s/.*<h1[^<>]*>//;s/<\/h1>.*//"`;
    rm -f "$TMPFILE"
    check_av_stream
    ms_title="$station_title"
    icy_name="$station_title"
    return $RC_OK
  fi

elif echo "${arg_url}" | grep -q -s '\(www\.\)*guzei\.com/online_tv/watch.php?online_tv_id=[0-9].*$'; then 

  local host_response=`$MSDL -q -o ${TMPFILE} -p http --useragent "${USERAGENT}" "${arg_url}" 2>&1`
  
  if [ -f ${TMPFILE} ]; then
    arg_url=`grep "player(" "$TMPFILE" | sed "s/.*(['\"]//;s/['\"],[^,]*,['\"].*//" | urldecode`;
    local player_type=`grep "player(" "$TMPFILE" | sed "s/.*,[\"']//;s/['\"].*//"`;
    local station_title=`grep "<h1" "$TMPFILE" | sed "s/.*<h1[^<>]*>//;s/<\/h1>.*//"`;
    rm -f "$TMPFILE"
    if [ "$player_type" == "WM" -o "$player_type" == "wm" ]; then
      check_av_stream
    elif [ "$player_type" == "flash" ]; then
      stream_type='video/x-flv'
      stream_url=`echo "${arg_url}" | sed 's/.*file=//;s/&.*//'`
      if echo "${arg_url}" | grep -q -s 'type=rtmp'; then 
        local streamer=`echo "${arg_url}" | sed 's/.*streamer=//;s/&.*//'`
        stream_url="$streamer/$stream_url"
        protocol='rtmp'
      elif echo "${arg_url}" | grep -q -s 'rtmp=rtmp:'; then 
        stream_url=`echo "${arg_url}" | sed 's/.*rtmp=//;s/&.*//'`
        protocol='rtmp'
      else
        arg_url="$stream_url"
        stream_url=
        check_av_stream
      fi
    fi
    ms_title="$station_title"
    icy_name="$station_title"
    return $RC_OK
  fi

fi

return $RC_FAIL
